import java.util.Random;

/**
 * 加减法口算练习系统-v0.2
 * 支持生成带答案的加减法题目，满足特定条件并多行多列显示
 */
public class CalculationSystemV02 {
    public static void main(String[] args) {
        // 输出系统标题
        System.out.println("===== 加减法口算练习系统-v0.2 =====");
        
        // 创建随机数生成器
        Random random = new Random();
        
        // 定义每行显示的题目数量
        final int PROBLEMS_PER_ROW = 2;
        
        // 用于存储每行的题目和答案
        String[][] problems = new String[50][PROBLEMS_PER_ROW];
        String[][] answers = new String[50][PROBLEMS_PER_ROW];
        
        // 生成50道题目
        for (int i = 0; i < 50; i++) {
            int row = i / PROBLEMS_PER_ROW;
            int col = i % PROBLEMS_PER_ROW;
            
            // 随机决定是加法还是减法
            boolean isAddition = random.nextBoolean();
            
            int num1, num2, result;
            
            if (isAddition) {
                // 加法：确保和不超过100
                num1 = random.nextInt(100) + 1; // 1到100
                // 确保第二个数加上第一个数不超过100
                num2 = random.nextInt(101 - num1); // 0到(100-num1)
                result = num1 + num2;
                
                // 存储题目和答案
                problems[row][col] = "第" + (i + 1) + "题: " + num1 + " + " + num2 + " = ";
                answers[row][col] = "答案: " + result;
            } else {
                // 减法：确保差不小于0
                num2 = random.nextInt(100); // 0到99
                // 确保第一个数大于等于第二个数
                num1 = num2 + random.nextInt(101 - num2); // num2到100
                result = num1 - num2;
                
                // 存储题目和答案
                problems[row][col] = "第" + (i + 1) + "题: " + num1 + " - " + num2 + " = ";
                answers[row][col] = "答案: " + result;
            }
        }
        
        // 输出题目，每行显示PROBLEMS_PER_ROW个题目
        System.out.println("\n===== 题目部分 =====");
        int totalRows = (int)Math.ceil(50.0 / PROBLEMS_PER_ROW);
        for (int i = 0; i < totalRows; i++) {
            StringBuilder problemRow = new StringBuilder();
            
            for (int j = 0; j < PROBLEMS_PER_ROW; j++) {
                if (i * PROBLEMS_PER_ROW + j < 50) {
                    // 格式化每个题目，确保对齐
                    problemRow.append(String.format("%-30s", problems[i][j]));
                }
            }
            
            System.out.println(problemRow.toString());
        }
        
        // 输出答案，每行显示PROBLEMS_PER_ROW个答案
        System.out.println("\n===== 答案部分 =====");
        for (int i = 0; i < totalRows; i++) {
            StringBuilder answerRow = new StringBuilder();
            
            for (int j = 0; j < PROBLEMS_PER_ROW; j++) {
                if (i * PROBLEMS_PER_ROW + j < 50) {
                    // 格式化每个答案，确保对齐
                    answerRow.append(String.format("%-30s", answers[i][j]));
                }
            }
            
            System.out.println(answerRow.toString());
        }
        
        System.out.println("\n===== 练习结束 =====");
    }
}